package mapEditor.view
{
	import asset.mapEditor.createFrame;
	
	import common.WindowView;
	
	import flash.events.MouseEvent;
	
	import mapEditor.events.MapEditorEvent;
	import mapEditor.model.EditorModel;
	
	public class CreateMapView extends WindowView
	{
		private var _model:EditorModel;
		private var _createFrame:createFrame;
		
		public function CreateMapView(model:EditorModel)
		{
			super(445, 280, "地图属性");
			_model=model;
			
			_createFrame=new createFrame();
			addChild(_createFrame);
			
			_createFrame.btnOk.buttonMode=_createFrame.btnClose.buttonMode=true;
			_createFrame.txtRow.text="20";
			_createFrame.txtCols.text="20";
			
			setEvent();
		}
		
		private function setEvent():void
		{
			_createFrame.btnOk.addEventListener(MouseEvent.CLICK, onClick);
			_createFrame.btnClose.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function onClick(evt:MouseEvent):void
		{
			switch(evt.type)
			{
				case MouseEvent.CLICK:
					switch(evt.currentTarget)
					{
						case _createFrame.btnOk:
							confirmCreate();
							break;
						case _createFrame.btnClose:
							closeView();
							break;			
					}
					break;
			}
		}
		
		private function confirmCreate():void
		{
			_model.mapVO.mapName=_createFrame.txtName.text;
			_model.mapVO.mapType=_createFrame.rbDiamond.selected ? 0 : 1;
			_model.mapVO.mapDescription=_createFrame.txtDescription.text;
			_model.mapVO.mapRow=int(_createFrame.txtRow.text);
			_model.mapVO.mapCols=int(_createFrame.txtCols.text);
			_model.mapVO.cellSize=int(_createFrame.txtCellSize.text);
			
			dispatchEvent(new MapEditorEvent(MapEditorEvent.MAP_NEW_CREATE));
			
			closeView();
		}
		
		private function closeView():void
		{
			dispose();
		}
		
		override public function dispose():void
		{
			super.dispose();
			_model=null;
			
			if(_createFrame) _createFrame.btnOk.removeEventListener(MouseEvent.CLICK, onClick);
			if(_createFrame) _createFrame.btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			
			if(_createFrame) _createFrame.parent.removeChild(_createFrame);
			_createFrame=null;
			if(parent) parent.removeChild(this);
		}
	}
}